package design_pattern.singleton;

/**
 * @Name : singleton.SingleTon03
 * @Description : 描述
 * Created by cyd on 2021-02-02 13:38:41
 */
public class SingleTon03 {
    public static void main(String[] args) {
        // 测试，拿到的实例是同一个对象
        Singlton03 instance = Singlton03.getInstance();
        Singlton03 instance1 = Singlton03.getInstance();
        System.out.println(instance == instance1);
    }
}

class Singlton03 {
    // 1.私有化构造器
    private Singlton03() {
    }

    // 2.本例内部声明对象实例
    private static Singlton03 instance;


    // 3.提供一个共有的静态方法，返回这个实例对象
    public static Singlton03 getInstance() {
        // 4.在返回前判断：如果没有被实例化，就在此处实例化
        if (instance == null) {
            instance = new Singlton03();
        }
        return instance;
    }
}
